package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.ConfirmBeen;
import cn.xfdzx.android.apps.shop.bean.GoodsUpStateBeen;
import cn.xfdzx.android.apps.shop.bean.ShopInGoodListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopOrderListBeen;
import cn.xfdzx.android.apps.shop.bean.UpdateNumPriceBeen;
import cn.xfdzx.android.apps.shop.contract.ShopInGoodListContract;
import cn.xfdzx.android.apps.shop.contract.ShopInOrderListContract;
import cn.xfdzx.android.apps.shop.model.ShopInGoodListModel;
import cn.xfdzx.android.apps.shop.model.ShopInOrderListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class ShopInOrderListPresenter extends BasePresenter<ShopInOrderListContract.View> implements ShopInOrderListContract.Presenter {

    private ShopInOrderListContract.Model model;

    public ShopInOrderListPresenter() {
        model = new ShopInOrderListModel();
    }


    @Override
    public void initData(boolean isRefresh, boolean isLoading, String type, String keyWord, String page, String limit) {
        if (!isViewAttached()) {
            return;
        }

        if (isLoading) {
            mView.showLoading();
        }
        model.initData(type,keyWord, page, limit)
                .compose(RxScheduler.<ShopOrderListBeen>Flo_io_main())
                .as(mView.<ShopOrderListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopOrderListBeen>() {
                    @Override
                    public void accept(ShopOrderListBeen bean) throws Exception {
                        mView.onOrderListSuccess(bean);
                        if (isLoading) {

                            mView.hideLoading();}

                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {

                            mView.hideLoading();}

                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void confirm(String order_id, int position) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.confirm(order_id)
                .compose(RxScheduler.<ConfirmBeen>Flo_io_main())
                .as(mView.<ConfirmBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmBeen>() {
                    @Override
                    public void accept(ConfirmBeen bean) throws Exception {
                        mView.onConfirmSuccess(bean, position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


}
